package com.th.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.th.common.domain.UsTask;
import com.th.common.domain.UsTaskRequire;
import com.th.common.exception.BaseServiceException;
import com.th.common.mybatis.query.QueryWrapperX;
import com.th.common.vo.UsTaskVo;
import com.th.mapper.UsTaskRequireMapper;
import com.th.service.UsTaskRequireService;
import com.th.service.UsTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.chrono.IsoChronology;
import java.util.Collections;
import java.util.List;

/**
 * @author ValcanoZz
 * @creat 2023-06-23-9:47
 */

@Service
public class UsTaskRequireServiceImpl extends ServiceImpl<UsTaskRequireMapper, UsTaskRequire> implements UsTaskRequireService {

    @Autowired
    private UsTaskRequireMapper usTaskRequireMapper;

    @Override
    public Boolean insertRequires(List<UsTaskRequire> usTaskRequire) {

        if (!this.saveBatch(usTaskRequire)) {
            throw new BaseServiceException(500, "当前任务的需求部分提交失败");
        } else {
            return true;
        }
    }

    @Override
    public List<UsTaskRequire> selectRequiresByTask(LambdaQueryWrapper<UsTaskRequire> wrapper) {
        return usTaskRequireMapper.selectList(wrapper);
    }

    @Override
    public List<UsTaskRequire> selectRequiresByTask(Long taskId) {
        QueryWrapper<UsTaskRequire> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UsTaskRequire::getTaskId, taskId);

        return usTaskRequireMapper.selectList(wrapper);
    }

    @Override
    public void updateRequires(List<UsTaskRequire> requires) {
        for (UsTaskRequire require : requires) {
            if (usTaskRequireMapper.updateById(require) <= 0) {
                throw new BaseServiceException(500, "任务需求为" + require.getId() + "的需求更新失败");
            }
        }
    }

    @Override
    public void updateOne(UsTaskRequire usTaskRequire) {
        if (usTaskRequireMapper.updateById(usTaskRequire) <= 0) {
            throw new BaseServiceException(500, "任务需求为" + usTaskRequire.getId() + "的需求更新失败");
        }
    }

    @Override
    public void deleteRequire(Integer id) {
        if (usTaskRequireMapper.deleteById(id) <= 0) {
            throw new BaseServiceException(500, "任务需求为" + id + "的需求删除失败");
        }
    }

    @Override
    public void deleteRequireByTaskId(Integer id) {
        if (usTaskRequireMapper.delete(new LambdaQueryWrapper<UsTaskRequire>().eq(UsTaskRequire::getTaskId, id)) <= 0) {
            throw new BaseServiceException(500, "任务为" + id + "的相关需求删除失败");
        }
    }
}
